//
//  AddScanFilesViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/16.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class AddScanFilesViewController: PurchaseTableViewController {

    var scanfiles: [JSON] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentation
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return super.tableView(tableView, numberOfRowsInSection: section)
        case 1:
            return scanfiles.count
        default:
            return self.documents.count
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 1:
            let footer = UIView()
            
            let topline = UIView()
            footer.addSubview(topline)
            topline.backgroundColor = tableView.separatorColor
            topline.snp.makeConstraints { (make) in
                make.left.equalTo(15)
                make.top.equalToSuperview()
                make.right.equalToSuperview()
                make.height.equalTo(1)
            }
            
            footer.backgroundColor = UIColor.dftBack
            
            footer.backgroundColor = UIColor.white
            
            let titleLbl = UILabel()
            
            titleLbl.textColor = UIColor.textColor
            
            titleLbl.font = UIFont.boldSystemFont(ofSize: 18)
            
            titleLbl.text = "合同扫描件:"
            
            titleLbl.frame = CGRect(origin: CGPoint(x: 15.0, y: 15.0), size: CGSize(width: titleLbl.contentSize.width, height: titleLbl.contentSize.height))
            
            footer.addSubview(titleLbl)

            return footer
        case 2:
            let footer = UIView()
            
            footer.backgroundColor = UIColor.dftBack
            
            footer.backgroundColor = UIColor.white
            
            let titleLbl = UILabel()
            
            titleLbl.textColor = UIColor.textColor
            
            titleLbl.font = UIFont.boldSystemFont(ofSize: 18)
            
            titleLbl.text = "附件:"
            
            titleLbl.frame = CGRect(origin: CGPoint(x: 15.0, y: 15.0), size: CGSize(width: titleLbl.contentSize.width, height: titleLbl.contentSize.height))
            
            footer.addSubview(titleLbl)
            
            let topline1 = UIView()
            footer.addSubview(topline1)
            topline1.backgroundColor = tableView.separatorColor
            topline1.snp.makeConstraints { (make) in
                make.left.equalTo(15)
                make.bottom.equalToSuperview()
                make.right.equalToSuperview()
                make.height.equalTo(1)
            }
            
            return footer
        default:
            return nil
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
            
        case 1:
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
            }
            
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < documents.count {
                cell?.textLabel?.text = scanfiles[indexPath.row].contents?["name"]?.rawString()
            }
            
            return cell!
        case 2:
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
            }
            
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < documents.count {
                cell?.textLabel?.text = self.documents[indexPath.row].contents?["name"]?.rawString()
            }
            
            return cell!
            
        default:
            return super.tableView(tableView, cellForRowAt: indexPath)
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.section {
        case 0:
            break
        case 1,2:
            var elements = documents;
            if indexPath.section == 1 {
                elements = scanfiles
            }
            let load = loadExcelViewController()
            
            if let id = elements[indexPath.row].contents?["id"]?.rawString() {
                
                let p = Server + filePath.rawValue + "?id=" + id
                
                NetManager.download(path: p, prog: { (progress) in
                    SVProgressHUD.showProgress(Float(progress))
                }) { (file) in
                    
                    SVProgressHUD.dismiss()
                    
                    let previewURL = PreviewURL(url: file, name: self.documents[indexPath.row].contents?["name"]?.rawString())
                    
                    load.path = previewURL
                    
                    self.present(load, animated: true, completion: nil)
                }
            }
            break
        default:
            break
        }
        
    }

}
